package com.sinothk.fc.goodsStock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.serialNumber.manager.SerialNumberHelper;
import com.sinothk.fc.goodsLibrary.entity.GoodsLibraryEntity;
import com.sinothk.fc.goodsLibrary.service.IGoodsLibraryService;
import com.sinothk.fc.goodsStock.entity.GoodsStockClassifyVO;
import com.sinothk.fc.goodsStock.entity.GoodsStockEntity;
import com.sinothk.fc.goodsStock.entity.GoodsStockSearchReq;
import com.sinothk.fc.goodsStock.mapper.GoodsStockMapper;
import com.sinothk.fc.goodsStock.service.IGoodsStockService;
import com.sinothk.fc.goodsStockRecord.entity.GoodsStockRecordEntity;
import com.sinothk.fc.goodsStockRecord.service.IGoodsStockRecordService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @Description: 库存信息
 * @Author: Liangyt
 * @Date: 2022-07-30
 * @Version: V1.0
 */
@Service
public class GoodsStockServiceImpl extends ServiceImpl<GoodsStockMapper, GoodsStockEntity> implements IGoodsStockService {
    @Resource
    private GoodsStockMapper goodsStockMapper;
    @Autowired
    private IGoodsLibraryService goodsLibraryService;
    @Autowired
    private IGoodsStockRecordService goodsStockRecordService;

    @Override
    public GoodsStockEntity addOrEdit(GoodsStockEntity goodsStockEntity) {
        String goodsUrl = goodsStockEntity.getGoodsUrl();
        if (StringUtils.isNotBlank(goodsUrl)) {
            // 封面获取
            String goodsCover = goodsUrl.split(",")[0];
            goodsStockEntity.setGoodsCover(goodsCover);
        }
        this.save(goodsStockEntity);

        // 处理商品模板
        GoodsLibraryEntity goodsLibraryEntity = new GoodsLibraryEntity();
        BeanUtils.copyProperties(goodsStockEntity, goodsLibraryEntity);
        goodsLibraryService.addOrEdit(goodsLibraryEntity);

        return goodsStockEntity;
    }

    @Override
    public IPage<GoodsStockEntity> storeGoodsStockPage(Page<GoodsStockEntity> page, GoodsStockEntity goodsReq) throws Exception {
        if (StringUtils.isBlank(goodsReq.getStoreId())) throw new Exception("storeId必传");
        LambdaQueryWrapper<GoodsStockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsStockEntity::getStoreId, goodsReq.getStoreId());
        if (goodsReq.getState() != -1) {
            queryWrapper.eq(GoodsStockEntity::getState, goodsReq.getState());
        }
        queryWrapper.orderByDesc(GoodsStockEntity::getCreateTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<GoodsStockClassifyVO> listForClassify(Page<GoodsStockEntity> page, GoodsStockSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());
        return goodsStockMapper.listForClassify(page, searchReq);
    }

    @Override
    public IPage<GoodsStockEntity> listForBarCode(Page<GoodsStockEntity> page, GoodsStockSearchReq searchReq) throws Exception {
        if (searchReq == null || org.apache.commons.lang3.StringUtils.isBlank(searchReq.getBarcode())) throw new Exception("商品条码必传");

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || org.apache.commons.lang3.StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());

        LambdaQueryWrapper<GoodsStockEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsStockEntity::getStoreId, loginUser.getStoreId())
                .eq(GoodsStockEntity::getGoodsNo, searchReq.getBarcode())
                .orderByAsc(GoodsStockEntity::getCpBzq);
        return goodsStockMapper.selectPage(page, wrapper);
    }

    @Override
    public GoodsStockClassifyVO getStockAllNumByBarcode(String storeId, String barcode) {
        return goodsStockMapper.getStockAllNumByBarcode(storeId, barcode);
    }

    @Override
    public void deleteBathStockForBarcode(String barcode) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

        LambdaQueryWrapper<GoodsStockEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsStockEntity::getStoreId, loginUser.getStoreId());
        wrapper.eq(GoodsStockEntity::getGoodsNo, barcode);
        this.remove(wrapper);
    }

    @Override
    public GoodsStockEntity getStockTotalInfoForBarcode(String barcode) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");

        LambdaQueryWrapper<GoodsStockEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsStockEntity::getStoreId, loginUser.getStoreId());
        wrapper.eq(GoodsStockEntity::getGoodsNo, barcode);

        IPage<GoodsStockEntity> pageData = this.page(new Page<>(1, 1), wrapper);
        GoodsStockEntity goodsStock = null;
        if (pageData != null && pageData.getRecords() != null && pageData.getRecords().size() > 0) {
            goodsStock = pageData.getRecords().get(0);
        } else {
            return null;
        }

        GoodsStockClassifyVO goodsStockClassifyVO = goodsStockMapper.getStockAllNumByBarcode(loginUser.getStoreId(), barcode);
        if (goodsStockClassifyVO == null || goodsStockClassifyVO.getGoodsAllNum() == null) {
            goodsStock.setStockNum(0);
        } else {
            goodsStock.setStockNum(goodsStockClassifyVO.getGoodsAllNum());
        }
        return goodsStock;
    }

    @Override
    public GoodsStockRecordEntity addStockNum(GoodsStockEntity beanReq) {
        // 首先更新库存
        beanReq.setStockNum(beanReq.getStockNum() + beanReq.getStockInputNum());
        this.updateById(beanReq);

        // 新增库存记录
        GoodsStockRecordEntity stockRecord = new GoodsStockRecordEntity();
        stockRecord.setStockId(beanReq.getId());
        stockRecord.setGoodsNo(beanReq.getGoodsNo());
        stockRecord.setGoodsInPrice(beanReq.getGoodsInPrice());
        stockRecord.setStockNum(beanReq.getStockInputNum());
        stockRecord.setGoodsAllPrice(beanReq.getGoodsInPrice() * beanReq.getStockInputNum());
        stockRecord.setState(1);// 状态：0未支付1已支付2不需支付
        stockRecord.setRemark("库存同款补货");

        stockRecord.setGoodsName(beanReq.getGoodsName());
        stockRecord.setGoodsCover(beanReq.getGoodsCover());
        stockRecord.setSellerId(beanReq.getSellerId());
        stockRecord.setSellerName(beanReq.getSellerName());
        stockRecord.setSellerLogo(beanReq.getSellerLogo());
        stockRecord.setSellerPhone(beanReq.getSellerPhone());
        goodsStockRecordService.save(stockRecord);

        return stockRecord;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<GoodsStockRecordEntity> addBatch(ArrayList<GoodsStockEntity> stockList) {
        // 新增物料
        ArrayList<GoodsStockRecordEntity> recordList = new ArrayList<>();
        for (GoodsStockEntity material : stockList) {
            // 设置识别号
            if (StringUtils.isBlank(material.getGoodsNo())) {
                material.setGoodsNo(SerialNumberHelper.getSerialNumber("产品"));
            }
        }
        this.saveBatch(stockList);

        // 新增入库记录
        for (GoodsStockEntity goodsStock : stockList) {
            // 设置记录
            GoodsStockRecordEntity stockRecord = new GoodsStockRecordEntity();
            stockRecord.setStockId(goodsStock.getId());
            stockRecord.setGoodsNo(goodsStock.getGoodsNo());
            stockRecord.setGoodsInPrice(goodsStock.getGoodsInPrice());
            stockRecord.setStockNum(goodsStock.getStockNum());
            stockRecord.setGoodsAllPrice(goodsStock.getGoodsInPrice() * goodsStock.getStockNum());
            stockRecord.setState(1);// 状态：0未支付1已支付2不需支付
            stockRecord.setRemark("新进库存");

            stockRecord.setGoodsName(goodsStock.getGoodsName());
            stockRecord.setGoodsCover(goodsStock.getGoodsCover());
            stockRecord.setSellerId(goodsStock.getSellerId());
            stockRecord.setSellerName(goodsStock.getSellerName());
            stockRecord.setSellerLogo(goodsStock.getSellerLogo());
            stockRecord.setSellerPhone(goodsStock.getSellerPhone());
            recordList.add(stockRecord);
        }
        goodsStockRecordService.saveBatch(recordList);
        return recordList;
    }
}
